﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Xml.Serialization;
using ObservadorPeplau.API.Business;

namespace ObservadorPeplau.API
{
    public static class SerializationManager
    {
        public static string SerializeToXml(object obj)
        {
            var serializer = new XmlSerializer(obj.GetType());
            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer, obj);

                return writer.ToString();
            }
        }

        public static object DeserializeFromXml(string xml, Type type)
        {
            var serializer = new XmlSerializer(type);

            //byte[] a = Encoding.GetEncoding("iso-8859-1").GetBytes(xml);
            byte[] a = System.Text.Encoding.Unicode.GetBytes(xml);
            var stream = new MemoryStream(a);

            var obj = serializer.Deserialize(stream);

            return obj;
        }
    }
}
